package com.shenqi.language.util;

import java.util.Date;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TimeParser {
    // 定义时间单位到Calendar常量的映射
    private static final Map<String, Integer> UNIT_MAP = new HashMap<>();

    static {
        UNIT_MAP.put("DAY", Calendar.DAY_OF_YEAR);
        UNIT_MAP.put("DAYS", Calendar.DAY_OF_YEAR);
        UNIT_MAP.put("MONTH", Calendar.MONTH);
        UNIT_MAP.put("MONTHS", Calendar.MONTH);
        UNIT_MAP.put("YEAR", Calendar.YEAR);
        UNIT_MAP.put("YEARS", Calendar.YEAR);
    }

    /**
     * 解析时间字符串并计算结束时间
     * @param timeStr 格式如 "3_MONTH", "1_YEAR", "30_DAY" 等
     * @param startDate 开始日期，如果为null或早于当前时间则使用当前时间
     * @return 结束时间的Date对象
     * @throws IllegalArgumentException 如果输入格式无效
     */
    public static Date calculateEndTime(String timeStr, Date startDate) {
        // 使用正则表达式解析字符串
        Pattern pattern = Pattern.compile("^(\\d+)_([A-Z]+)$");
        Matcher matcher = pattern.matcher(timeStr);

        if (!matcher.matches()) {
            throw new IllegalArgumentException("无效的时间格式。应为类似'3_MONTH'的格式");
        }

        int amount = Integer.parseInt(matcher.group(1));
        String unitStr = matcher.group(2);

        Integer unit = UNIT_MAP.get(unitStr);
        if (unit == null) {
            throw new IllegalArgumentException("未知的时间单位: " + unitStr +
                    "。支持的单位: DAY, DAYS, MONTH, MONTHS, YEAR, YEARS");
        }

        Date now = new Date();
        Date effectiveStartDate = startDate;

        // 如果startDate为null或早于当前时间，则使用当前时间
        if (startDate == null || startDate.before(now)) {
            effectiveStartDate = now;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(effectiveStartDate);
        calendar.add(unit, amount);

        return calendar.getTime();
    }

    public static void main(String[] args) {
        // 测试示例
        String[] testCases = {"3_MONTH", "1_YEAR", "30_DAY", "2_MONTHS", "1_YEARS"};

        // 创建一个未来的日期用于测试
        Calendar futureCal = Calendar.getInstance();
        futureCal.add(Calendar.DAY_OF_YEAR, 10);
        Date futureDate = futureCal.getTime();

        // 创建一个过去的日期用于测试
        Calendar pastCal = Calendar.getInstance();
        pastCal.add(Calendar.DAY_OF_YEAR, -10);
        Date pastDate = pastCal.getTime();

        System.out.println("当前时间: " + new Date());
        System.out.println("测试用未来日期: " + futureDate);
        System.out.println("测试用过去日期: " + pastDate);
        System.out.println("=====================================");

        for (String testCase : testCases) {
            try {
                // 测试1: startDate为null
                Date endTime1 = calculateEndTime(testCase, null);
                System.out.println("输入: " + testCase + ", startDate: null");
                System.out.println("结束时间: " + endTime1);

                // 测试2: startDate为过去日期
                Date endTime2 = calculateEndTime(testCase, pastDate);
                System.out.println("输入: " + testCase + ", startDate: 过去日期");
                System.out.println("结束时间: " + endTime2);

                // 测试3: startDate为未来日期
                Date endTime3 = calculateEndTime(testCase, futureDate);
                System.out.println("输入: " + testCase + ", startDate: 未来日期");
                System.out.println("结束时间: " + endTime3);

                System.out.println("----------------------");
            } catch (IllegalArgumentException e) {
                System.out.println("处理 '" + testCase + "' 时出错: " + e.getMessage());
            }
        }
    }
}